package com.esri.android.map;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.core.app.NotificationCompat;
import com.esri.android.map.a;
import com.esri.android.map.ags.ArcGISPopupInfo;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.map.popup.Popup;
import com.esri.android.map.popup.PopupLayer;
import com.esri.android.map.popup.PopupLayoutInfo;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.RuntimeHelper;
import com.esri.core.internal.tasks.d;
import com.esri.core.internal.tasks.e;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.Feature;
import com.esri.core.portal.WebMapLayer;
import com.github.mikephil.charting.utils.Utils;
import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

/* loaded from: classes.dex */
public abstract class Layer {
    private String a;
    private SpatialReference b;
    private Envelope c;
    private String d;
    String j;
    int l;
    protected volatile long nativeHandle;
    OnStatusChangedListener o;
    protected Map<Integer, ArcGISPopupInfo> popUpInfos;
    private Future<?> t;
    private WeakReference<WebMapLayerLoader> w;
    private WeakReference<WebMapLayer> x;
    private String z;
    boolean h = true;
    float i = 1.0f;
    Map<Integer, String> k = null;
    private double e = Double.NaN;
    private double f = Double.NaN;
    private volatile boolean g = false;
    MapSurface m = null;
    private final AtomicInteger s = new AtomicInteger(0);
    transient Handler n = null;
    protected UserCredentials credentials = null;
    private boolean u = false;
    private boolean v = true;
    protected Object serviceInfo = null;
    Layer q = null;
    private boolean y = false;
    float r = -1.0f;
    private int A = 0;
    final transient Handler p = c();

    protected abstract long create();

    protected abstract void initLayer();

    native double[] nativeGetFullExtent(long j);

    native double nativeGetMaxScale(long j);

    native double nativeGetMinScale(long j);

    native float nativeGetRenderPixelsPerInch(long j);

    native long nativeLock(long j);

    native void nativeRelease(long j);

    native void nativeSetInitialExtent(long j, double d, double d2, double d3, double d4);

    native void nativeSetMaxScale(long j, double d);

    native void nativeSetMinScale(long j, double d);

    native void nativeSetName(long j, String str);

    public native void nativeSetOpacity(long j, float f);

    native void nativeSetRenderPixelsPerInch(long j, float f);

    native void nativeSetVisible(long j, boolean z);

    native long nativeUnlock(long j);

    native double[] navtiveGetInitialExtent(long j);

    protected boolean verifyCredentials() {
        return false;
    }

    static {
        RuntimeHelper.initialize();
    }

    public Layer(boolean z) {
        this.nativeHandle = 0L;
        if (z) {
            this.nativeHandle = create();
        }
    }

    public Layer() {
        this.nativeHandle = 0L;
        this.nativeHandle = create();
    }

    public Layer(long j) {
        this.nativeHandle = 0L;
        this.nativeHandle = j;
    }

    private Handler c() {
        return new Handler(Looper.getMainLooper(), new Handler.Callback() { // from class: com.esri.android.map.Layer.1
            {
                Layer.this = this;
            }

            @Override // android.os.Handler.Callback
            public boolean handleMessage(Message message) {
                if (Layer.this.o != null) {
                    Layer.this.o.onStatusChanged(Layer.this, OnStatusChangedListener.STATUS.fromInt(message.getData().getInt(NotificationCompat.CATEGORY_STATUS)));
                    return true;
                }
                return false;
            }
        });
    }

    public void a(Object obj) {
        this.serviceInfo = obj;
    }

    Object a() {
        return this.serviceInfo;
    }

    public void a(WebMapLayerLoader webMapLayerLoader, WebMapLayer webMapLayer) {
        this.w = new WeakReference<>(webMapLayerLoader);
        this.x = new WeakReference<>(webMapLayer);
        if (webMapLayer != null) {
            this.z = webMapLayer.getID();
        }
    }

    public <T> T loadServiceInfo(Callable<T> callable) throws Exception {
        WeakReference<WebMapLayer> weakReference = this.x;
        WebMapLayer webMapLayer = weakReference == null ? null : weakReference.get();
        WeakReference<WebMapLayerLoader> weakReference2 = this.w;
        WebMapLayerLoader webMapLayerLoader = weakReference2 == null ? null : weakReference2.get();
        if (webMapLayer == null || webMapLayerLoader == null) {
            return (T) ((d) callable).execute();
        }
        a.C0012a<T> loadServiceInfo = webMapLayerLoader.loadServiceInfo((d) callable, this, webMapLayer);
        if (loadServiceInfo == null) {
            return null;
        }
        this.credentials = loadServiceInfo.b;
        return loadServiceInfo.a;
    }

    public void reinitializeLayer(UserCredentials userCredentials) {
        setCredentials(userCredentials);
        this.serviceInfo = null;
        b();
    }

    public void b() {
        try {
            this.t = getServiceExecutor().submit(new Runnable() { // from class: com.esri.android.map.Layer.2
                {
                    Layer.this = this;
                }

                @Override // java.lang.Runnable
                public void run() {
                    Layer.this.initLayer();
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    public void waitForInitialized() throws InterruptedException, ExecutionException {
        Future<?> future = this.t;
        if (future != null) {
            future.get();
        }
    }

    public void finalize() throws Throwable {
        recycle();
    }

    public void recycle() {
        synchronized (this) {
            if (this.nativeHandle != 0) {
                long j = this.nativeHandle;
                this.nativeHandle = 0L;
                nativeRelease(j);
                this.g = true;
            }
        }
    }

    public boolean isRecycled() {
        return this.g;
    }

    public boolean isVisible() {
        return this.h;
    }

    public void setVisible(boolean z) {
        if (this.nativeHandle != 0) {
            nativeSetVisible(this.nativeHandle, z);
            this.h = z;
        }
    }

    public float getOpacity() {
        return this.i;
    }

    public void setOpacity(float f) {
        if (this.nativeHandle != 0) {
            nativeSetOpacity(this.nativeHandle, f);
            this.i = f;
        }
    }

    public void setOnStatusChangedListener(OnStatusChangedListener onStatusChangedListener) {
        this.o = onStatusChangedListener;
    }

    public void changeStatus(OnStatusChangedListener.STATUS status) {
        c(status == OnStatusChangedListener.STATUS.INITIALIZED);
        a(this.p, status);
        if (status != OnStatusChangedListener.STATUS.INITIALIZED) {
            a(this.n, OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.STATUS.LAYER_LOADING_FAILED.getValue(), status.getError()));
        }
    }

    private void a(Handler handler, OnStatusChangedListener.STATUS status) {
        if (handler != null) {
            Message obtainMessage = handler.obtainMessage();
            Bundle bundle = new Bundle();
            bundle.putInt(NotificationCompat.CATEGORY_STATUS, status.getValue());
            obtainMessage.setData(bundle);
            obtainMessage.obj = this;
            handler.sendMessage(obtainMessage);
        }
    }

    private void c(boolean z) {
        this.s.set(z ? 1 : -1);
    }

    public boolean isInitialized() {
        return this.s.get() == 1;
    }

    public String getName() {
        return this.d;
    }

    public void setName(String str) {
        this.d = str;
        if (this.nativeHandle == 0 || str == null || str.length() <= 0) {
            return;
        }
        nativeSetName(this.nativeHandle, str);
    }

    public void setUrl(String str) {
        if (str != null) {
            this.j = str;
            this.l = str.hashCode();
        }
    }

    public String getUrl() {
        return this.j;
    }

    public void setQueryUrls(Map<Integer, String> map) {
        this.k = map;
    }

    public String getQueryUrl(int i) {
        Map<Integer, String> map = this.k;
        if (map == null) {
            return null;
        }
        return map.get(Integer.valueOf(i));
    }

    public int getUrlHashCode() {
        return this.l;
    }

    public void setInitialExtent(Envelope envelope) {
        if (this.nativeHandle == 0 || envelope == null) {
            return;
        }
        nativeSetInitialExtent(this.nativeHandle, envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax());
    }

    public UserCredentials getCredentials() {
        UserCredentials userCredentials = this.credentials;
        if (userCredentials == null) {
            return null;
        }
        return userCredentials.getCopy();
    }

    public void setCredentials(UserCredentials userCredentials) {
        this.credentials = userCredentials == null ? null : userCredentials.getCopy();
    }

    public Envelope getInitialExtent() {
        double[] navtiveGetInitialExtent;
        if (this.nativeHandle == 0 || (navtiveGetInitialExtent = navtiveGetInitialExtent(this.nativeHandle)) == null || navtiveGetInitialExtent.length != 4) {
            return null;
        }
        return new Envelope(navtiveGetInitialExtent[0], navtiveGetInitialExtent[1], navtiveGetInitialExtent[2], navtiveGetInitialExtent[3]);
    }

    public void setFullExtent(Envelope envelope) {
        this.c = envelope;
    }

    public Polygon getExtent() {
        MapSurface mapSurface = this.m;
        if (mapSurface != null) {
            return mapSurface.i();
        }
        return null;
    }

    public Envelope getFullExtent() {
        double[] nativeGetFullExtent;
        Envelope envelope = this.c;
        if (envelope != null && !envelope.isEmpty()) {
            return this.c;
        }
        if (this.nativeHandle == 0 || (nativeGetFullExtent = nativeGetFullExtent(this.nativeHandle)) == null || nativeGetFullExtent.length != 4) {
            return null;
        }
        return new Envelope(nativeGetFullExtent[0], nativeGetFullExtent[1], nativeGetFullExtent[2], nativeGetFullExtent[3]);
    }

    public void updateSpatialReferenceStatus(int i) {
        WeakReference<WebMapLayer> weakReference = this.x;
        WebMapLayer webMapLayer = weakReference == null ? null : weakReference.get();
        WeakReference<WebMapLayerLoader> weakReference2 = this.w;
        WebMapLayerLoader webMapLayerLoader = weakReference2 != null ? weakReference2.get() : null;
        if (webMapLayerLoader != null) {
            webMapLayerLoader.invokeOnWebMapLoadListener(webMapLayer, this, this.credentials);
        }
        if (i > 0) {
            a(this.n, OnStatusChangedListener.STATUS.LAYER_LOADED);
        } else if (i == 0) {
            a(this.n, OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.SPATIAL_REFERENCE_INVALID));
        } else {
            a(this.n, OnStatusChangedListener.STATUS.LAYER_LOADING_FAILED);
        }
        int i2 = this.A;
        if (i2 > 0) {
            b(i2);
        }
    }

    public void b(int i) {
        this.A = i;
        MapSurface mapSurface = this.m;
        if (mapSurface == null || i <= 0) {
            return;
        }
        mapSurface.b(i);
    }

    public long getID() {
        return this.nativeHandle;
    }

    public String getWebMapLayerID() {
        return this.z;
    }

    public String getTitle() {
        return this.a;
    }

    public void a(String str) {
        this.a = str;
    }

    public Popup createPopup(MapView mapView, int i, Feature feature) {
        return createPopup(mapView, i, feature, null, true);
    }

    public Popup createPopup(MapView mapView, int i, Feature feature, PopupLayoutInfo popupLayoutInfo, boolean z) {
        ArcGISPopupInfo popupInfo = getPopupInfo(i);
        if (popupInfo == null) {
            return null;
        }
        Popup popup = new Popup(mapView, popupInfo, feature, popupLayoutInfo, z, getCredentials());
        boolean z2 = this instanceof PopupLayer;
        popup.setEditable(z2 ? ((PopupLayer) this).isPopupEditable(feature) : false);
        popup.setDeletable(z2 ? ((PopupLayer) this).isPopupDeletable(feature) : false);
        popup.setAllowGeometryUpdate(z2 ? ((PopupLayer) this).isPopupAllowGeometryUpdate(feature) : false);
        return popup;
    }

    public ArcGISPopupInfo getPopupInfo(int i) {
        Map<Integer, ArcGISPopupInfo> map = this.popUpInfos;
        if (map == null) {
            return null;
        }
        return map.get(Integer.valueOf(i));
    }

    public Map<Integer, ArcGISPopupInfo> getPopupInfos() {
        return this.popUpInfos;
    }

    public void setPopupInfos(Map<Integer, ArcGISPopupInfo> map) {
        this.popUpInfos = map;
    }

    public SpatialReference getSpatialReference() {
        MapSurface mapSurface = this.m;
        if (mapSurface == null) {
            return null;
        }
        return mapSurface.j();
    }

    public double getResolution() {
        MapSurface mapSurface = this.m;
        if (mapSurface == null) {
            return Double.NaN;
        }
        return mapSurface.f();
    }

    public int getWidth() {
        MapSurface mapSurface = this.m;
        if (mapSurface == null) {
            return 0;
        }
        return mapSurface.getWidth();
    }

    public int getHeight() {
        MapSurface mapSurface = this.m;
        if (mapSurface == null) {
            return 0;
        }
        return mapSurface.getHeight();
    }

    public SpatialReference getDefaultSpatialReference() {
        return this.b;
    }

    public boolean isWebMapBaselayer() {
        return this.u;
    }

    public void a(boolean z) {
        this.u = z;
    }

    public boolean isShowLegend() {
        Layer layer = this.q;
        if (layer != null) {
            return this.v && a(layer);
        }
        return this.v;
    }

    private boolean a(Layer layer) {
        if (layer.getParent() != null) {
            return a(layer.getParent());
        }
        return layer.v;
    }

    public void b(boolean z) {
        this.v = z;
    }

    public Layer getParent() {
        return this.q;
    }

    public double getMaxScale() {
        return this.nativeHandle == 0 ? this.f : nativeGetMaxScale(this.nativeHandle);
    }

    public double getMinScale() {
        return this.nativeHandle == 0 ? this.e : nativeGetMinScale(this.nativeHandle);
    }

    public void setMaxScale(double d) {
        if (this.nativeHandle != 0 && d > Utils.DOUBLE_EPSILON) {
            nativeSetMaxScale(this.nativeHandle, d);
        }
        this.f = d;
    }

    public void setMinScale(double d) {
        if (this.nativeHandle != 0 && d > Utils.DOUBLE_EPSILON) {
            nativeSetMinScale(this.nativeHandle, d);
        }
        this.e = d;
    }

    public void setDefaultSpatialReference(SpatialReference spatialReference) {
        this.b = spatialReference;
    }

    public float getDpi() {
        if (this.y) {
            MapSurface mapSurface = this.m;
            if (mapSurface != null) {
                return mapSurface.p();
            }
            return 0.0f;
        }
        if (this.r < 0.0f) {
            this.r = nativeGetRenderPixelsPerInch(this.nativeHandle);
        }
        return this.r;
    }

    protected synchronized long lock() {
        if (this.nativeHandle != 0) {
            return nativeLock(this.nativeHandle);
        }
        return 0L;
    }

    protected void unlock(long j) {
        if (j != 0) {
            nativeUnlock(j);
        }
    }

    public void initializeMinMaxScale(double d, double d2) {
        if (!Double.isNaN(this.e)) {
            d = this.e;
        }
        if (!Double.isNaN(d)) {
            setMinScale(d);
        }
        if (!Double.isNaN(this.f)) {
            d2 = this.f;
        }
        if (Double.isNaN(d2)) {
            return;
        }
        setMaxScale(d2);
    }

    public ExecutorService getPoolExecutor() {
        MapSurface mapSurface = this.m;
        return mapSurface != null ? mapSurface.i.b : e.b;
    }

    public void setRenderNativeResolution(boolean z) {
        this.y = z;
    }

    public boolean isRenderNativeResolution() {
        return this.y;
    }

    public ExecutorService getServiceExecutor() {
        MapSurface mapSurface = this.m;
        return mapSurface != null ? mapSurface.i.c : e.c;
    }

    public void setRenderPixelsPerInch(float f) {
        if (this.nativeHandle != 0) {
            nativeSetRenderPixelsPerInch(this.nativeHandle, f);
        }
    }
}
